﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Drawing;
using System.Configuration;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.IO;
using Intorise.Common;

namespace Bot
{
    public class BotHelper
    {
        static readonly string ocrExecPath = ConfigurationManager.AppSettings["TesseractPath"];

        static Dictionary<Guid, bool> quitDict = new Dictionary<Guid, bool>();
        static Dictionary<Guid, DateTime> timeDict = new Dictionary<Guid, DateTime>();

        /// <summary>
        /// 定时执行指定的任务
        /// </summary>
        /// <param name="id">此任务的执行标识</param>
        /// <param name="milliseconds">毫秒</param>
        public static void TimespanRunThread(Action action, int milliseconds, Guid id)
        {
            lock (quitDict)
            {
                if (quitDict.ContainsKey(id))
                {
                    return;
                }
            }
            Task.Factory.StartNew(() =>
            {
                while (quitDict[id])
                {
                    Thread.Sleep(milliseconds);
                    if (!quitDict[id]) break;
                    action();
                }
            });
        }

        /// <summary>
        /// 定时执行指定的任务
        /// </summary>
        /// <param name="id">此任务的执行标识</param>
        /// <param name="milliseconds">毫秒</param>
        public static void TimespanRun(Action action, int milliseconds, Guid id)
        {
            lock (timeDict)
            {
                if (!timeDict.ContainsKey(id))
                {
                    action();
                    timeDict.Add(id, DateTime.Now); return;
                }
            }
            if ((DateTime.Now - timeDict[id]).TotalMilliseconds >= milliseconds)
            {
                action();
                timeDict[id] = DateTime.Now;
            }
        }

        /// <summary>
        /// 指示停止定时执行指定的任务
        /// </summary>
        /// <param name="id">此任务的执行标识</param>
        public static void StopRunThread(Guid id)
        {
            lock (quitDict)
            {
                if (quitDict.ContainsKey(id))
                {
                    quitDict[id] = false;
                }
            }
        }

        public static string OcrScan(Image image)
        {
            string text = "";
            string path = AppDomain.CurrentDomain.BaseDirectory;
            string imageFilename = Guid.NewGuid() + ".bmp";
            image.Save(path + imageFilename, ImageFormat.Bmp);
            File.Delete(path + "result.txt");

            Process p = new Process();
            p.StartInfo = new ProcessStartInfo(ocrExecPath);
            p.StartInfo.Arguments = path + imageFilename + " " + path + "result  -psm 7  digits";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.Start();
            var result = p.StandardOutput.ReadToEnd();
            ConsoleBLL.WriteLine(result);
            p.WaitForExit();

            text = File.ReadAllText(path + "result.txt");
            File.Delete(path + imageFilename);
            return text;
        }
    }
}
